//
//  QuanziDetailViewController.m
//  YiqiMobile
//
//  Created by henry on 13-3-24.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "HuodongTagViewController.h"
#import "Utility.h"
#import "Config.h"
#import "JSON.h"
#import "DefineAction.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"


@implementation HuodongTagViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    self.navigationItem.titleView=[Utility getTitleView:@"编辑标签"];
    //导航栏右边按钮
     self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(onClickNavAction:) normalImage:@"cg_eidt_normal.png" lightedImage:@"cg_eidt_hover.png" frame:CGRectMake(0, 0, 58, 29) title:nil tag:0]];
    

    float tableHeight=UISCREEN_HEIGHT-20-49-44;
    self.mytableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, tableHeight) style:UITableViewStylePlain];
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    //self.mytableView.showsVerticalScrollIndicator=NO;
    self.mytableView.delegate=self;
    self.mytableView.dataSource=self;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
    self.arrayList[0]=@{@"head":@"tagtougao.png",@"list":@[
                       @{@"content":@"社会创新",@"id":@"1"},
                       @{@"content":@"思想文化",@"id":@"2"},
                       @{@"content":@"健康生活",@"id":@"4"}
                       ]};
    self.arrayList[1]=@{@"head":@"tagquanzi.png",@"list":@[
                       @{@"content":@"技术",@"id":@"4"},
                       @{@"content":@"小资",@"id":@"5"},
                       @{@"content":@"咖啡",@"id":@"6"}
                       ]};
    // Do any additional setup after loading the view from its nib.
    
      
}
-(void) onClickNavAction:(UIButton*) sender{
    if (self.mytableView.editing) {
           self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(onClickNavAction:) normalImage:@"cg_eidt_normal.png" lightedImage:@"cg_eidt_hover.png" frame:CGRectMake(0, 0, 58, 29) title:nil tag:0]];
    }else{
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(onClickNavAction:) normalImage:@"tagedit_finish_normal.png" lightedImage:@"tagedit_finish_hover.png" frame:CGRectMake(0, 0, 58, 29) title:nil tag:0]];
    }
    self.mytableView.editing=!self.mytableView.editing;
}


-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void) loadData:(NSDictionary*) param{
    
    //[self loadUserInfoFromServer];
}

-(void) loadUserInfoFromServer{
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (app.status==NotReachable) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
        return;
    }
    NSString *userId=[[NSUserDefaults standardUserDefaults] objectForKey:USERID];
    NSString *url=[NSString stringWithFormat:@"%@/%@?id=%@&u.u0=%@",[Utility  getServerUrl],GETUSERINFOACTION,userId,userId];
    self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:nil andFile:nil];
       [[MBProgressHUD showHUDAddedTo:self.view animated:YES] setLabelText:LOADING_TITLE_DEFAULT];
    [self.request startLoadData];
}

- (void)requestFinished:(id)context{
    [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[[result objectForKey:@"jr"] objectForKey:@"code"] integerValue]==0) {
            
            if ([[[result objectForKey:@"jr"] objectForKey:@"method"] isEqualToString:GETUSERINFOACTION]) {
                
            }
            
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:NO];
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}



//表视图委托
#pragma mark - Tableview datasource & delegates
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIImageView *view=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 29)];
    view.image=[Utility imageNamed:[[self.arrayList objectAtIndex:section] objectForKey:@"head"]];
    return view;
}

//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.arrayList count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[[self.arrayList objectAtIndex:section] objectForKey:@"list"] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 29.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentity = @"huodongTagViewCell";
    UITableViewCell *cell = nil;
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity];
            if (cell ==  nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
            }
    NSDictionary *dict=[[[self.arrayList objectAtIndex:indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row];
    cell.textLabel.text=[dict objectForKey:@"content"];
    cell.textLabel.font=[UIFont  fontWithName:FONT_FAMILY size:15.0f];
            return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",@"xianzhong");
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
